package com.sjgs.gis.web;

import com.sjgs.gis.domain.Chunk;
import com.sjgs.gis.service.ChunkService;
import com.sjgs.gis.errors.BadRequestAlertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;


@RestController
@RequestMapping("/api/chunk")
public class ChunkController {

    @Autowired
    private ChunkService service;

    @GetMapping("")
    public Flux<Chunk> findAll() {
        return this.service.findAll();
    }

    @PostMapping(value="",produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<Chunk> insert(@RequestBody  Chunk chunk) {
        if (chunk.getId() != null || chunk.getFkey() == null || chunk.getIndex() == null) {
            throw new BadRequestAlertException("Invalid chunk", "Chunk", "fkye and index must be not null");
        }
        return this.service.save(chunk);
    }

    @PutMapping(value="",produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<Chunk> update(@RequestBody  Chunk chunk) {
        if (chunk.getId() == null || chunk.getFkey() == null || chunk.getIndex() == null) {
            throw new BadRequestAlertException("Invalid chunk", "Chunk", "fkye and index must be not null");
        }
        return this.service.save(chunk);
    }

    @DeleteMapping(value="",produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<Void> delete(@RequestBody  Chunk chunk) {
        if (chunk.getId() == null || chunk.getFkey() == null || chunk.getIndex() == null) {
            throw new BadRequestAlertException("Invalid chunk", "Chunk", "fkye and index must be not null");
        }
        return this.service.delete(chunk);
    }
}